/**
 * Created by xiaolinzhang on 2017/7/30.
 */
var mysqlConn = require("./BaseDao");

exports.insToken = function (token,times,callbk) {
    var sql = 'insert into token_info(access_token,expire_time) values (?,DATE_ADD(now(),INTERVAL ? SECOND))';
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[token,times],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.getToken = function (callbk) {
    var sql = 'select access_token from token_info where now() < expire_time';
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.getShareUrl = function (userId,callbk) {
    var sql = 'select ticket from user_share_info where user_id=? and now() < expires_time';
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[userId],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}

exports.insShareUrl = function (userId,ticket,expire_seconds,url,callbk) {
    var sql = 'insert into user_share_info(user_id,ticket,share_url,expires_time) values(?,?,?,DATE_ADD(now(),INTERVAL ? SECOND))';
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[userId,ticket,url,expire_seconds],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.insJsTicket = function (jsticket,expire_seconds,callbk) {
    var sql = 'insert into js_ticket_info (jsticket,expire_time,create_time) values(?,DATE_ADD(now(),INTERVAL ? SECOND),now())';
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[jsticket,expire_seconds],function (err,result) {
            connection.release();
            console.log(err);
            callbk(err,result);
        })
    });
}

exports.getJsTicket = function (callbk) {
    var sql = 'select jsticket from js_ticket_info where now() < expire_time';
    mysqlConn.getConn(function (err,connection) {
        connection.query(sql,[],function (err,rows) {
            connection.release();
            console.log(err);
            callbk(err,rows);
        })
    });
}